// pages/addinfo/addinfo.js
const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    list:[],
    page: 1,//页码
    limit: 5,//每页数量
    type:0,//可不传参数
    keyword:"",//可不传参数
    add_time:"",//可不传参数
    p_no:"",//项目号
    count: 0,
    projecthead:[],//项目基础信息
    projectbody:[],//项目主体信息
    datastatus:0,//查看 0全部/1准备中/2进行中/3未完成 
    datafind:"",//搜索关键字
    date:"",
    mindate:Math.round((new Date() - 365*86400*1000)),
    findtime:"",//时间范围查找
    tmpstatus:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.p_no = options.p_no
    this.getList()
  },

  onReachBottom() {
    if(this.data.flag){
      this.setData({
        flag:false
      })
      this.getList(); // 疯狂的请求的方法
    }
  },
  
  getList(search="",date="") {
    var that = this;
    var params = {
      type:that.data.datastatus,
      page: that.data.page,//当前页码1
      limit: that.data.limit,
      p_no:that.data.p_no,
      keyword:that.data.datafind,//关键字
      add_time:that.data.findtime,//时间范围
      date:date,
      search:that.data.datafind,
      select:Number(that.data.select),
    }
    //页面功能定义
    request.requestGet('order/get_project_info', params, "加载中",function (res) {
      // console.log(res,"resssssssssss")
      if (res.status === 200) {
        let list = that.data.projectbody
        // that.setData({projecthead:res.data.project_info})
        res.data.data.map((item) =>{
          return item.t = false;
        })
        if (that.data.page == 1) {
          list = []
        }
        list = [...list, ...res.data.data]
        if (list.length < res.data.count ){
          that.setData({
            page: that.data.page + 1,
            flag:true
          })
        } else {
          that.setData({
            flag:false
          })
        }
        that.setData({
          projectbody: list,
          count: res.data.count
        })
      }
      }, function (res) {
        console.log(res,"请求失败")
      },
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  submit(event){
  },
  bindKeyInputVant(event){
    let valueName = event.target.dataset.variable;
    let name = `formData.${valueName}`;
    this.setData({
      [valueName]:event.detail
    });
  },
  onTabChange(event){
    if(event.detail.index === 1){
      this.data.datastatus = 1
    }else if(event.detail.index === 2){
      this.data.datastatus = 2
    }else if(event.detail.index === 0){
      this.data.datastatus = 0
    }else if(event.detail.index === 3){
      this.data.datastatus = 3
    }
    this.data.page = 1
    this.getList()
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.data.page = 1
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
    this.getList("",this.data.date)
  },

  onSearch(e){
    this.setData({
      flag:true
    })
    this.data.page = 1
    this.data.datafind = e.detail
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    this.getList(e.detail)
  },
  onReachBottom() {
    if(this.data.flag){
      this.setData({
        flag:false
      })
      this.getList(); // 疯狂的请求的方法
    }
  },
  comment(event){
    let tmpindex = event.currentTarget.dataset.index
    let pic_no = 0
    let p_no = 0
    for(let i = 0;i<this.data.projectbody.length;i++){
      if(this.data.projectbody[i].id === tmpindex){
        pic_no = this.data.projectbody[i].pic_no
        p_no = this.data.projectbody[i].p_no
      }
    }
    wx.navigateTo({
      url: `/pages/process_list/process_list?id=${tmpindex}&pic_no=${pic_no}&p_no=${p_no}`,
    })
  },
  edit(event){
    let tmp = event.currentTarget.dataset.index
    wx.navigateTo({
      url: `/pages/set_order_info/set_order_info?p_no=${tmp}`,
    })
  },
  delete(e){
    let params = {
      id:e.currentTarget.dataset.index
    }
    var that = this
    request.requestGet('order/del', params, "加载中",function (res) {
      if (res.status === 200) {
        that.getList()
        } else {
          that.setData({
            flag:false
          })
        }
      }, function (res) {
        console.log(res,"请求失败")
      },
    )
  },
  need_set_pay(e){
    let id = e.currentTarget.dataset.id
    let material_no = e.currentTarget.dataset.material_no
    let num = e.currentTarget.dataset.num
    wx.navigateTo({
      url: `/pages/need_set_pay/need_set_pay?id=${id}&material_no=${material_no}&num=${num}`,
    })
  },
  del_need_pay(e){
    var that = this
    let params = {
      id:e.currentTarget.dataset.id
    }
    request.requestGet('order/removePayTicket', params, "删除中",function (res) {
      if (res.status === 200) {
        that.getList()
        } else {
          wx.showToast({
            title: res.msg,
            icon:'none'
          })
        }
      }, function (res) {
        console.log(res,"请求失败")
      },
    )
  },
  flow_chart(e){
    console.log(e,"eeeeeeeee")
    let tmpid = e.currentTarget.dataset.id
    wx.navigateTo({ url: `/pages/flow_chart/flow_chart?id=${tmpid}` })
  }
})